package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.more.MorePrefUtil;
import com.stark.more.MoreUiUtil;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivitySettingBinding;
import flc.ast.dialog.HideModeDialog;
import flc.ast.dialog.SetAnswerDialog;
import flc.ast.dialog.SetPasswordDialog;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.event.usersys.UserSysEventProxy;
import stark.common.basic.utils.SPUtil;

public class SettingActivity extends BaseAc<ActivitySettingBinding> {

    //是否开启隐蔽模式
    private boolean isHideMode;

    @Override
    protected int onCreate() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.ivSettingBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivSettingsRecom.setOnClickListener(this);
        mDataBinding.ivUserCenter.setOnClickListener(this);
        mDataBinding.ivHideModeInfo.setOnClickListener(this);
        mDataBinding.ivHideMode.setOnClickListener(this);
        mDataBinding.ivPasswordManage.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSettingsRecom:
                boolean isOpened = MoreUiUtil.isPersonalRecommendOpened();
                MoreUiUtil.setPersonalRecommendOpened(!isOpened);
                if (isOpened) {
                    mDataBinding.ivSettingsRecom.setImageResource(R.drawable.aguan);
                } else {
                    mDataBinding.ivSettingsRecom.setImageResource(R.drawable.akai);
                }
                break;
            case R.id.ivUserCenter:
                UserSysEventProxy.getInstance().goUserCenter(this);
                break;
            case R.id.ivHideModeInfo://隐蔽模式
                HideModeDialog dialog = new HideModeDialog(mContext);
                dialog.show();
                break;
            case R.id.ivHideMode:
                isHideMode = !isHideMode;
                if (isHideMode) {
                    mDataBinding.ivHideMode.setImageResource(R.drawable.akai);
                } else {
                    mDataBinding.ivHideMode.setImageResource(R.drawable.aguan);
                }
                SPUtil.putBoolean(mContext, Content.isHideMode, isHideMode);
                break;
            case R.id.ivPasswordManage://密码管理
                //是否完成设置密码
                boolean isSetPassword = SPUtil.getBoolean(mContext, Content.isHavePassword, false);
                if (isSetPassword) {
                    answerDialog(true);
                } else {
                    ToastUtils.showShort(getString(R.string.please_set_password));
                }
                break;
        }
    }

    @Override
    protected void initData() {
        boolean isOpened = MoreUiUtil.isPersonalRecommendOpened();
        if (isOpened) {
            //将控件开关设为开
            mDataBinding.ivSettingsRecom.setImageResource(R.drawable.akai);
        } else {
            //将控件开关设为关
            mDataBinding.ivSettingsRecom.setImageResource(R.drawable.aguan);
        }
        boolean showPersonalRecommend = MorePrefUtil.showPersonalRecommend();
        if (showPersonalRecommend) {
            //显示个性化开关控件
            mDataBinding.rlSettingsRecom.setVisibility(View.VISIBLE);
        } else {
            //隐藏个性化开关控件
            mDataBinding.rlSettingsRecom.setVisibility(View.GONE);
        }
        mDataBinding.ivUserCenter.setVisibility(UserSysEventProxy.getInstance().supportUserSys() ? View.VISIBLE : View.GONE);
        //是否开启隐蔽模式
        isHideMode = SPUtil.getBoolean(mContext, Content.isHideMode, false);
        if (isHideMode) {
            mDataBinding.ivHideMode.setImageResource(R.drawable.akai);
        } else {
            mDataBinding.ivHideMode.setImageResource(R.drawable.aguan);
        }
    }

    private void answerDialog(boolean isForget) {
        SetAnswerDialog answerDialog = new SetAnswerDialog(mContext);
        answerDialog.isForget = isForget;
        answerDialog.setListener(new SetAnswerDialog.Listener() {
            @Override
            public void setPassword(String question, String answer) {
                passwordDialog(true, question, answer);
            }
        });
        answerDialog.show();
    }

    private void passwordDialog(boolean isSetPassword, String question, String answer) {
        SetPasswordDialog passwordDialog = new SetPasswordDialog(mContext);
        passwordDialog.isFile = true;
        passwordDialog.isSetPassword = isSetPassword;
        if (isSetPassword) {
            passwordDialog.question = question;
            passwordDialog.answer = answer;
        }
        passwordDialog.setListener(new SetPasswordDialog.Listener() {
            @Override
            public void forgetPassword() {
                answerDialog(true);
            }
        });
        passwordDialog.show();
    }

}